// 角色溶解
Shader "Study/Chapter3/RoleDissolve"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
		_DissolveTex("DissTex",2D) = "white" {}
		_Dissolve("DissolValue",Range(0,1.1)) = 0
		_DissolveRamp("DissolRamp",2D) = "white" {}
		_Width("Width",Range(0,0.15)) = 0.1
		[Toggle(USE_ICE_TEX)]_UseIce("UseIce",int) = 0
		_IceTex("IceTex",2D) = "white" {}
		_IceVal("IceVal",Range(0,3)) = 1.2
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
			#pragma multi_compile _ USE_ICE_TEX
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
				float2 uv2 : TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
			sampler2D _DissolveTex;
			float4 _DissolveTex_ST;
			sampler2D _DissolveRamp;
			sampler2D _IceTex;
			float4 _IceTex_ST;
			float _Dissolve;
			float _Width;
			float _IceVal;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
				o.uv.zw = TRANSFORM_TEX(v.uv, _DissolveTex);
				#if USE_ICE_TEX
					o.uv2 = TRANSFORM_TEX(v.uv, _IceTex);
				#endif
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 mainTex = tex2D(_MainTex, i.uv.xy);
				fixed4 dissolveTex = tex2D(_DissolveTex,i.uv.zw);
				#if USE_ICE_TEX

				#else
					clip(dissolveTex.r - _Dissolve);	
				#endif
               
				fixed4 col = fixed4(1,1,1,1);
				
				#if USE_ICE_TEX
					fixed4 iceTex = tex2D(_IceTex,i.uv2);
					fixed val = step(dissolveTex.r,_Dissolve);
					//fixed4 iceColor = val * iceTex;
					col =  mainTex * (1-val) +  val * (mainTex * (1-_IceVal) + iceTex * _IceVal) ;
				#else
					fixed2 rampUV = smoothstep(_Dissolve,_Dissolve + _Width,dissolveTex.r);
					fixed4 dissRamp = tex2D(_DissolveRamp,rampUV);
					col = mainTex + dissRamp;
				#endif

                return col;
            }
            ENDCG
        }
    }
}
